define(function (require,exports,module) {
    var angular = require('angular');
    var $ = require('jquery');

    angular.module('directives.menu',[])
        .directive('followmenu',[
            '$compile',
            function ($compile) {
                return {
                    restrict: "A",
                    scope: {ngModel: '='},
                    require:'^ngModel',
                    controller: [
                        '$scope',
                        function ($scope) {
                            $scope.cancel = function () {
                                $scope.show = false;
                                $scope.ngModel.cancel && $scope.ngModel.cancel();
                            };
                            $scope.ok = function () {
                                var text = $scope.text;
                                if ($scope.ngModel.followType !== 'text') {
                                    if($scope.ngModel.validate(text)) {
                                        $scope.show = false;
                                        $scope.ngModel.ok && $scope.ngModel.ok(text,$scope.followmenu);
                                    }
                                } else {
                                    $scope.show = false;
                                    $scope.ngModel.ok && $scope.ngModel.ok($scope.followmenu);
                                }
                            };
                            $scope.$on("$destroy",function(){
                                $('#followMenu').remove();
                            })
                        }
                    ],
                    link: function (scope,element,attr) {
                        function getTplByType(type){
                            if(type == 'text'){
                                return '<div class="content">{{ngModel.title}}</div>';
                            }else if(type == 'radio'){
                                return '<label class="form-radio {{ngModel.currId==group.id?\'selected\':\'\'}}" ng-repeat="group in ngModel.getGroups()" ng-click="ngModel.currId = group.id">' +
                                    '<i class="icon-radio"></i>' +
                                    '<span class="align-m">{{group.name}}</span>' +
                                    '</label>'
                            }else{
                                return '<div class="content form">' +
                                    '<span>{{ngModel.title}}</span>' +
                                    '<input type="text" class="form-control" ng-model="text">' +
                                    '</div>';
                            }
                        }
                        var menuTpl = ['<div class="pt pt-up" style="display:block;" ng-show="show" id="followMenu">',
                            '<div class="cont" style="width: 286px; height: auto;">',
                                '<div class="tips-wrap">',
                                    getTplByType(scope.ngModel.followType),
                                    '<div class="clearfix">',
                                        '<button type="button" ng-click="ok()" class="btn-pr ok-btn pull-left">确定</button>',
                                        '<button type="button" ng-click="cancel()" class="btn-pr cancel-btn pull-right">取消</button>',
                                    '</div>',
                                '</div>',
                            '</div>',
                            '<b class="out" style="top: -8px; left: 136px; right: auto; bottom: auto;"></b>',
                            '<b class="in" style="top: -7px; left: 137px; bottom: auto; right: auto;"></b>',
                        '</div>'].join('');

                        var showMenu = function (container) {
                            var followMenu = document.getElementById('followMenu');
                            if (followMenu) {
                                $(followMenu).remove();
                            }
                            scope.show = true;
                            if (scope.ngModel.followType !== 'text') {
                                scope.text = scope.ngModel.text(scope.followmenu);
                            }
                            followMenu = $compile(menuTpl)(scope);
                            //angular.element(container).parent().append(followMenu);
                            $(document.body).append(followMenu);

                            container = $(container);
                            followMenu = $(followMenu);
                            var offset = container.offset();

                            followMenu.css({
                                'left': offset.left - (followMenu.width() - container.width()) / 2 + 'px',
                                'top': offset.top + parseInt(container.height()) + 5 + 'px'
                            });
                        };

                        scope.followmenu = attr.followmenu;
                        element.on('click',function () {
                            scope.$apply(function () {
                                showMenu(element);
                            });
                        });
                    }
                };
            }
        ]);
});
